package com.sui.bigdata.sml.web.controller;

import com.sui.bigdata.sml.web.dto.ProjectDTO;
import com.sui.bigdata.sml.web.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author songhaicheng
 * @date 2019/11/5 15:09
 * @description
 * @reviewer
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/web/ui/projects")
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectService service;

    @PostMapping
    ResponseEntity add(@RequestBody ProjectDTO dto) {
        return new ResponseEntity<>(dto.setId(service.add(dto)), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    ResponseEntity update(@PathVariable("id") Long id, @RequestBody ProjectDTO dto) {
        dto.setId(id);
        service.update(dto);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping
    ResponseEntity list() {
        return new ResponseEntity<>(service.listAll(), HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    ResponseEntity delete(@PathVariable("id") Long id) {
        service.delete(id);
        log.info("项目删除成功，id：{}", id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
